﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;
using BaseSet.Entity.Common.DataOperation;

namespace WebSite.Admin.CashManager
{
    public partial class CashFlowList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadContent(false);

                // 从BasePage中得到
                PageLiteral.Text = BuildPageLinks();
            }
        }


        #region 加载列表
        private void LoadContent(bool mark)
        {

            //设置控件属性，从BasePage中得到
            PageConfig(CashFlowUPager);

            LoadCondition();

            int type = Convert.ToInt32(ddlType.SelectedValue);
            string begindate = txtBeginDate.Text.Trim();
            string enddate = txtEndDate.Text.Trim();

            //List<Member> list = new List<Member();
            CashFlowHelper cfhelper = new CashFlowHelper();
            Criteria c = new Criteria();
            if (type != -1)
            {
                if (type == 1)
                {
                    c.Add(CriteriaType.MoreThan, "Money", 0);
                }
                else if (type == 2)
                {
                    c.Add(CriteriaType.LessThan, "Money", 0);
                }
            }
            
            if (begindate != "")
            {
                c.Add(CriteriaType.MoreThanEquals, "CreateDate", begindate);
            }
            if (enddate != "")
            {
                c.Add(CriteriaType.LessThanEquals, "CreateDate", enddate);
            }
            CashFlowUPager.ItemCount = cfhelper.QueryCashFlowCountBySearch(c);

            // 以下的值从BasePage中得到
            CashFlowUPager.UrlFormat = UrlFormat;
            CashFlowUPager.PrefixText = GetPreFixText(CashFlowUPager);

            if (mark)
            {
                PagerInit(CashFlowUPager);
            }

            string[] fields = new string[] { "ID", "UsersID", "Money", "Description", "CreateDate" };

            DataTable dt = new DataTable();
            dt = cfhelper.QueryCashFlowBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, CashFlowUPager.Begin - 1, CashFlowUPager.Count, fields);

            dt.Columns.Add(new DataColumn("username"));
            dt.Columns.Add(new DataColumn("nickname"));
            dt.Columns.Add(new DataColumn("moneytype"));
            dt.Columns.Add(new DataColumn("strcreatedate"));

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string usersid = dr["usersid"] + "";
                    //查找用户名和昵称
                    Users uinfo = new Users();
                    UsersHelper uhelper = new UsersHelper();
                    uinfo = uhelper.GetUsers(usersid);

                    dr["username"] = uinfo.UserName;
                    dr["nickname"] = uinfo.NickName;

                    dr["strcreatedate"] = Convert.ToDateTime(dr["createdate"]).ToString("yyyy-MM-dd");

                    string money = dr["money"] + "";
                    double dmoney = Convert.ToDouble(money);
                    string moneytype = "";
                    if (dmoney > 0)
                    {
                        moneytype = "进账";
                    }
                    else if (dmoney<0)
                    {
                        moneytype = "出账";
                    }
                    dr["moneytype"] = moneytype;
                }
            }

            rptCashFlow.DataSource = dt;
            rptCashFlow.DataBind();
        }
        #endregion

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            SetCondition();
            LoadContent(true);
        }

        #region 查询条件设置
        private void SetCondition()
        {
            int type = Convert.ToInt32(ddlType.SelectedValue);
            string begindate = txtBeginDate.Text.Trim();
            string enddate = txtEndDate.Text.Trim();

            //将查询条件放入Session中
            Session["QueryList"] = type + "@" + begindate + "@" + enddate + "@" + "CashFlowList";

        }

        private void LoadCondition()
        {
            string condition = Session["QueryList"] + "";

            string[] conditionAll = condition.Split('@');
            if (conditionAll.Length == 4)
            {
                string page = conditionAll[3];
                if (page == "CashFlowList")
                {
                    ddlType.SelectedValue = conditionAll[0];
                    txtBeginDate.Text = conditionAll[1];
                    txtEndDate.Text = conditionAll[2];
                }
            }
        }
        #endregion

        #region 导出
        protected void btnExport_Click(object sender, EventArgs e)
        {
            int type = Convert.ToInt32(ddlType.SelectedValue);
            string begindate = txtBeginDate.Text.Trim();
            string enddate = txtEndDate.Text.Trim();

            //List<Member> list = new List<Member();
            CashFlowHelper cfhelper = new CashFlowHelper();
            Criteria c = new Criteria();
            if (type != -1)
            {
                if (type == 1)
                {
                    c.Add(CriteriaType.MoreThan, "Money", 0);
                }
                else if (type == 2)
                {
                    c.Add(CriteriaType.LessThan, "Money", 0);
                }
            }

            if (begindate != "")
            {
                c.Add(CriteriaType.MoreThanEquals, "CreateDate", begindate);
            }
            if (enddate != "")
            {
                c.Add(CriteriaType.LessThanEquals, "CreateDate", enddate);
            }

            string[] fields = new string[] { "ID", "UsersID", "Money", "Description", "CreateDate" };

            DataTable dt = new DataTable();
            dt = cfhelper.QueryCashFlowBySearchToTable(c, new Order[] { new Order("CreateDate") }, 0, 0, fields);


            dt.Columns.Add(new DataColumn("No"));
            dt.Columns.Add(new DataColumn("username"));
            dt.Columns.Add(new DataColumn("nickname"));
            dt.Columns.Add(new DataColumn("moneytype"));
            dt.Columns.Add(new DataColumn("strcreatedate"));

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string usersid = dr["usersid"] + "";
                    //查找用户名和昵称
                    Users uinfo = new Users();
                    UsersHelper uhelper = new UsersHelper();
                    uinfo = uhelper.GetUsers(usersid);

                    dr["username"] = uinfo.UserName;
                    dr["nickname"] = uinfo.NickName;

                    dr["strcreatedate"] = Convert.ToDateTime(dr["createdate"]).ToString("yyyy-MM-dd");

                    string money = dr["money"] + "";
                    string moneytype = "";
                    if (money.Contains("+"))
                    {
                        moneytype = "进账";
                    }
                    else if (money.Contains("-"))
                    {
                        moneytype = "出账";
                    }
                    dr["moneytype"] = moneytype;
                }

                string[] excelcolumnname = new string[] { "用户ID", "用户名", "昵称", "金额", "现金方向","描述", "日期" };
                string[] fieldcolumn = new string[] { "UsersID", "username", "nickname", "money", "moneytype", "Description", "strCreateDate" };

                string filename = "CashFlow_" + System.DateTime.Now;

                if (!FileOperation.ExportToExcel(Page, filename, dt, excelcolumnname, fieldcolumn, false))
                {
                    Tools.Alert("导出失败！");
                }
            }
            else
            {
                Tools.Alert("导出数据为空！");
            }
        }
        #endregion
    }
}